package org.platform.processor.knowledge;


import com.alibaba.fastjson.JSON;
import com.mc.mic.core.exception.IllegalParamException;
import lombok.extern.slf4j.Slf4j;
import org.platform.constant.DiagnoseTypeConstant;
import org.platform.mo.csmo.cmd10022.KnowApplyRequestParam;
import org.platform.mo.csmo.common.*;
import org.platform.processor.IBaseProcessor;
import org.platform.tool.util.ComUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author FengJie
 */
@Service
@Slf4j
public class KnowledgePublishReceiveProcessor implements IBaseProcessor {
    @Autowired
    private KnowledgeProcessor processor;

    /**
     * 初始化发布内容(主要修改ID)
     *
     * @param requestParam
     * @return
     */
    public KnowApplyRequestParam knowPublishInit(KnowApplyRequestParam requestParam) {
        String knowledgeCtx = requestParam.getKnowledgeCtx();
        KB_Main main = requestParam.getKbMain();
        main.setId(ComUtils.getUUID());

        switch (requestParam.getKnowledgeType()) {
            case DiagnoseTypeConstant.KNOW_MONITOR:
                //TODO 故障检测点暂时不处理,后期需要处理
                break;
            case DiagnoseTypeConstant.KNOW_TEST:
                KnowTestApplyBean testApplyBean = JSON.parseObject(knowledgeCtx, KnowTestApplyBean.class);
                KB_Test testMain = testApplyBean.getTestMain();
                testMain.setId(ComUtils.getUUID());

                List<KB_TestChannel> testChannelList = testApplyBean.getTestChannelList();
                for (KB_TestChannel channel : testChannelList) {
                    channel.setId(ComUtils.getUUID());
                }

                List<KB_TestParam> testParamList = testApplyBean.getTestParamList();
                for (KB_TestParam param : testParamList) {
                    param.setId(ComUtils.getUUID());
                }
                requestParam.setKnowledgeCtx(JSON.toJSONString(testApplyBean));
                break;
            case DiagnoseTypeConstant.KNOW_TREE:
                KnowTreeApplyBean treeApplyBean = JSON.parseObject(knowledgeCtx, KnowTreeApplyBean.class);
                List<KB_TreeNode> treeNodeList = treeApplyBean.getTreeNodeList();
                for (KB_TreeNode node : treeNodeList) {
                    //node.setId(ComUtils.getUUID());
                }
                // todo 删除老版本的,不然会id重复
                KB_Tree treeMain = treeApplyBean.getTreeMain();
                treeMain.setId(ComUtils.getUUID());
                requestParam.setKnowledgeCtx(JSON.toJSONString(treeApplyBean));
                break;
            default:
                throw new IllegalParamException("申请类型不正确 :未找到指定的知识申请类型");
        }
        return requestParam;
    }


    /**
     * 处理知识发布流程
     *
     * @param publishData
     */
    public void knowledgePublish(String publishData) {
        KnowApplyRequestParam publish = JSON.parseObject(publishData, KnowApplyRequestParam.class);
        processor.addKnowledge(publish);
    }

    /**
     * 获取对应的发布知识
     *
     * @param main
     * @return
     */
    public KnowApplyRequestParam getKnowledge(KB_Main main) {
        return processor.getKnowledgeByMain(main);
    }

}
